import requests
# r = requests.get('https://api.github.com/events')
# print(r)
# r = requests.post('http://httpbin.org/post', data = {'key': 'value'})
# print(r)
#
# payload = {'name': 'John','age': 19}
# r = requests.get('http://httpbin.org/get', params = payload)
# print(r.url) # http://httpbin.org/get?name=John&age=19
#
# payload = {'key1': 'value1', 'key2': ['value2', 'value3']}
# r = requests.get('http://httpbin.org/get', params = payload)
# print(r.url) # http://httpbin.org/get?key1=value1&key2=value2&key2=value3
#
# r = requests.get('https://api.github.com/events')
# print(r.text)
# print(r.encoding)

url = 'https://www.baidu.com'
response = requests.get(url)
#
# # 手动设定编码格式
# response.encoding = 'utf-8'
# # 打印源码的str类型数据
# print(response.text)
#
# # response.content是存储的bytes类型的响应源码，可以进行decode操作
# print(response.content.decode('utf-8'))
#

# 常见的响应参数和方法
# 响应url
# print(response.url)

# 状态码
# print(response.status_code)

# 响应对象的请求头
# print(response.request.headers)
# 响应对象的响应头
# print(response.headers)

# 响应设置cookie
# print(response.cookies) # <RequestsCookieJar[<Cookie BDORZ=27315 for .baidu.com/>]>

# print(len(response.content.decode('utf-8')))
# print(response.content.decode('utf-8'))

# 构建请求头字典
# headers = {
#     'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36'
# }

# 发送带请求头的请求
# response1 = requests.get(url, headers=headers)
# print(len(response1.content.decode('utf-8')))
# print(response1.content.decode('utf-8'))


# 设置超时时间
url = "https://twitter.com"
try:
    response2 = requests.get(url, timeout=3)
except Exception as e:
    print(e)


